home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK2.toast / Development Kits (Disc 2) / QuickDraw GX / Programming Stuff / Sample Code / Printing Samples / Applications… / Getting Started w⁄GX ƒ / Getting Started GX - main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-20  |  10.2 KB  |  349 lines  |  [TEXT/KAHL]

  1. /**
  2.   --
  3.  --        App:        Getting Started w/QD GX (WWDC)
  4.  --
  5.  -- 
  6.  --        Version:    1.0     4/93:    added all of the calls required to support the "Getting 
  7.  --                                    Started with QuickDraw™ GX" session at the WWDC '93     
  8.  --
  9.  --                            8/93:    updated file to work with the ß2 "GXified" interface files
  10.  --                            9/93:    now overrides gxPrintingEvent for update event handling.
  11.  --                                    added GXUpdateJob call to resume event handling. - dmh
  12.  --                            5/94:    gxPrintingEvent override now ignores appropriate events. - dmh
  13.  --            
  14.  --        File:        Getting Started GX - main.c
  15.  --
  16.  --
  17.  --        Comments:    This file is a part of a shell that can be used to build QuickDraw GX  
  18.  --                    applications. It contains all of the required calls to: testing for 
  19.  --                    QuickDraw GX, gxInitialize the QuickDraw GX world, and handling events.
  20.  --        
  21.  --                 For this shell to work correctly, it expects the following functions to
  22.  --                    be defined:
  23.  --
  24.  --                    void DoInitialization(WindowPtr);
  25.  --                    void DoDraw(WindowPtr);
  26.  --                    void DoDispose(WindowPtr);
  27.  --                    void DoClick(WindowPtr);
  28.  --                    void DoIdle(WindowPtr);
  29.  --
  30.  --                    By the way, if the QuickDraw GX printing system is not installed, we will
  31.  --                    not enable the print menu items in th File menu. The app will still draw
  32.  --                    QuickDraw GX shapes to the window, but it will not print them.
  33.  --
  34.  --
  35.  --
  36.  --        Components:    Getting Started GX - main.c
  37.  --                    Getting Started GX - main.h
  38.  --                    Getting Started GX - shapes.c
  39.  --                    Getting Started GX - printing.c
  40.  --                    Getting Started GX - misc.c
  41.  --                    Getting Started QD GX.π.rsrc
  42.  --
  43.  --                    The file titled: "Getting Started GX - main.c" contains the code required to
  44.  --                    intialize and tear down the QuickDraw GX world, and the event loop.
  45.  --
  46.  --                    The file titled: "Getting Started GX - shapes.c" contains of the code used to 
  47.  --                    create and manipulate the shapes draw into the window.
  48.  --        
  49.  --                    The file titled: "Getting Started GX - printing.c" contains of the code used to 
  50.  --                    print the contents of  the window.
  51.  --        
  52.  --                    The file titled: "Getting Started GX - misc.c" contains of the code for the utilities used.
  53.  --        
  54.  --        
  55.  --        QuickDraw GX
  56.  --        Libraries
  57.  --        Used:        This application uses the following QuickDraw GX library code files:
  58.  --                    "gxColor library.c", "gxFont library.c", "graphics debug library.c",
  59.  --                    "layout library.c", "qd library.c", "gxShape library.c", 
  60.  --                    "gxTransferMode library.c", and "gxTransform library.c". 
  61.  --        
  62.  --        
  63.  --        Notes:        1) Print this file in landscape for the best results
  64.  --                    2) If you are using THINK C v5.x, I have added THINK markers to navigate the code.
  65.  --                    3) This code was adapted from the "Banana Jr." QuickDraw GX sample.
  66.  --
  67.  --
  68.  --        Author:        Pete "Luke" Alexander
  69.  --                    Developer Technical Support
  70.  --                    AppleLink: DEVSUPPORT
  71.  --
  72.  --        
  73.  --        ©1990 - 1993  Apple Computer, Inc. 
  74.  --        All rights reserved.
  75.  --
  76.  **/
  77.  
  78.  
  79. #include <Desk.h>
  80. #include <Events.h>
  81. #include <Fonts.h>
  82. #include <Windows.h>
  83. #include <Memory.h>
  84. #include <ToolUtils.h>
  85. #include <Menus.h>
  86. #include <Quickdraw.h>
  87.  
  88. #include <GXPrinting.h>
  89. #include <GXEnvironment.h>
  90. #include <GXGraphics.h>
  91. #include "GraphicsLibraries.h"
  92. #include <GXErrors.h>
  93. #include "Getting Started GX - main.h"
  94.  
  95. #define    kOSEvent                        app4Evt    /* event used by MultiFinder */
  96. #define    kSuspendResumeMessage            1        /* high byte of suspend/resume event message */
  97. #define    kResumeMask                        1        /* bit of message field for resume vs. suspend */
  98.  
  99. Boolean            gQuitting;
  100.  
  101. gxStyle            gOurStyle;
  102. gxColor         gTextColor;
  103. gxTransform        gRotatedTransform;
  104. gxPoint            gTextLocation = {ff(75), ff(105)};
  105. long            gSleep = 0;
  106.  
  107. /*------ main -----------------------------------------------------------------------------------------*/
  108.  
  109. void main()
  110. {        
  111.     CursHandle            theCurs; 
  112.     Handle                menuBar;
  113.     gxGraphicsClient     client;
  114.     OSErr                err;
  115.     WindowPtr            wind;
  116.     
  117.     /**     
  118.         The GXNewGraphicsClient routine defines the graphics heap size. If you do not make this call, 
  119.         the GX graphics engine will create this heap automatically. How? It will create a heap which 
  120.         is a percentage of your application's ideal memory foot print. This call allows you to explicity 
  121.         define the ammount of memory used by the graphics system for it's graphics objects heap.
  122.     **/
  123.     client = GXNewGraphicsClient(nil, gGraphicsHeapSize * 1024, 0L);
  124.  
  125.     /**   Generic heap initialization.  **/
  126.     MaxApplZone(); 
  127.     MoreMasters(); MoreMasters(); MoreMasters(); 
  128.     MoreMasters(); MoreMasters(); MoreMasters(); 
  129.  
  130.     /** 
  131.         If gDebugging = TRUE, you will receive graphics library errors & notices will be posted.  This
  132.         functionality will only work with the "debugging" version of the QuickDraw GX init.  If this
  133.         init is not installed, these functions will not work. 
  134.     **/
  135.     if (gDebugging) {
  136.         SetGraphicsLibraryErrors ();
  137.         SetGraphicsLibraryNotices();    
  138.     }
  139.  
  140.     /** 
  141.         Set  "gGiveMeValidation" to TRUE, if you want run-time validation. As you increase the amount
  142.         of validation, The drawing speed will SLOW down due to all of the internal checking. 
  143.         
  144.         publicValidation will check parameters to public routines. For additional details regarding 
  145.         the various levels of validation, please see the documentation.
  146.     **/
  147.     if (gGiveMeValidation) GXSetValidation(gxPublicValidation); 
  148.  
  149.  
  150.     /** initialize the new graphics and printing environments.  **/
  151.  
  152.     GXEnterGraphics();
  153.     err = GXInitPrinting();
  154.  
  155.     /**   initialize the other managers, if no errors occured.   **/
  156.      
  157.      if (!err)
  158.      {
  159.          InitGraf(&qd.thePort);
  160.         InitFonts();
  161.         InitWindows();
  162.         InitMenus();
  163.         InitCursor();
  164.  
  165.         theCurs = GetCursor(watchCursor);
  166.         SetCursor(*theCurs);
  167.  
  168.         menuBar = GetNewMBar(rMenuBar); /* Create the menu bar */
  169.  
  170.         if (menuBar)
  171.         {
  172.             gQuitting = false;
  173.             SetMenuBar(menuBar);
  174.             DisposHandle(menuBar);
  175.             AddResMenu(GetMHandle(mApple), 'DRVR'); /* add Apple Menu items */
  176.             DrawMenuBar();
  177.  
  178.      // We initialize the CommonColors Library.  This will allow us to set the color of a
  179.      // shape by calling the SetShapeCommonColor function. We will need to call
  180.      // DisposeCommonColors when we leave, to clean up the world.
  181.  
  182.               InitCommonColors();
  183.     
  184.             DoCreateNew();
  185.             SetCursor(&qd.arrow);  
  186.             
  187.             while (!gQuitting)
  188.             {
  189.                 EventLoop();
  190.                 DoIdle(wind);
  191.             }
  192.  
  193.  
  194.     // Leaving.  Close all the windows so we get rid of any data we or GX created.  Then,
  195.     // dispose of the common colors and exit the GX printing and graphics environment.
  196.  
  197.             while (wind = FrontWindow())
  198.                 DoDispose(wind);
  199.  
  200.             DisposeCommonColors();
  201.             if (gOurStyle != nil) GXDisposeStyle (gOurStyle);
  202.             if (gRotatedTransform != nil) GXDisposeTransform (gRotatedTransform);
  203.         }
  204.  
  205.         GXExitPrinting();    /** Close the new printing mgr. **/
  206.     }
  207.     
  208.     GXExitGraphics();        /** Deallocate all of the default structures **/
  209.     GXDisposeGraphicsClient(client);
  210. }
  211.  
  212.  
  213. /*------ MyPrintingEventOverride -----------------------------------------------------------------------------*/
  214. // Override for GXPrintingEvent.  It allows us to update our windows
  215. // when the moveable modal printing dialogs are moved.
  216.  
  217. OSErr MyPrintingEventOverride(EventRecord *anEvent, Boolean filterEvent)
  218. {
  219.     OSErr    err = noErr;
  220.  
  221.     // Handle events in whatever way is appropriate.  MyDoEvent is our
  222.     // generic event handler.  We don't pass it events that it shouldn't
  223.     // handle while print dialogs are displayed.
  224.  
  225.     if (!filterEvent)
  226.         switch (anEvent->what)
  227.         {
  228.             case mouseDown:
  229.             case keyDown:
  230.             case autoKey:
  231.                 break;
  232.             
  233.             default:
  234.                 MyDoEvent(anEvent);
  235.         }
  236.  
  237.     return err;
  238. }
  239.  
  240.  
  241. /*------ EventLoop ------------------------------------------------------------------------------------*/
  242.  
  243. void EventLoop()
  244. {
  245.     EventRecord        theEvent;
  246.  
  247.     if (WaitNextEvent(everyEvent, &theEvent, gSleep, nil))
  248.         MyDoEvent(&theEvent);
  249. }
  250.  
  251.  
  252.  
  253. /*------ MyDoEvent ------------------------------------------------------------------------------------*/
  254.  
  255. void MyDoEvent(EventRecord *theEvent)
  256. {
  257.     char            key;
  258.     WindowPtr         whichWindow;
  259.     GrafPtr            oldPort;
  260.  
  261.     switch(theEvent->what)
  262.     {                    
  263.         case updateEvt:
  264.             if (((WindowPeek) theEvent->message)->windowKind == userKind)
  265.             {
  266.                 GetPort(&oldPort);
  267.                 SetPort((WindowPtr) theEvent->message);
  268.                 BeginUpdate((WindowPtr) theEvent->message);
  269.                 DoDraw((WindowPtr) theEvent->message);
  270.                 EndUpdate((WindowPtr) theEvent->message);
  271.                 SetPort(oldPort);
  272.               }
  273.         break;
  274.         
  275.         case mouseDown:
  276.         switch (FindWindow(theEvent->where, &whichWindow)) {
  277.             case inSysWindow:
  278.                 SystemClick(theEvent, whichWindow);
  279.             break;
  280.                     
  281.             case inDrag:
  282.                 if (((WindowPeek) whichWindow)->windowKind == userKind)
  283.                     DragWindow(whichWindow, theEvent->where, &qd.screenBits.bounds);
  284.             break;
  285.  
  286.             case inGoAway:
  287.                 if (((WindowPeek) whichWindow)->windowKind == userKind)
  288.                 {
  289.                     if (TrackGoAway(whichWindow, theEvent->where))
  290.                         DoDispose(whichWindow);
  291.                 }
  292.             break;
  293.             
  294.             case inContent:
  295.                 if ((whichWindow != FrontWindow()) &&
  296.                     (((WindowPeek) whichWindow)->windowKind == userKind))
  297.                      SelectWindow(whichWindow);
  298.             break;
  299.             case inMenuBar:
  300.                 DoMenuCommand(MenuSelect(theEvent->where));
  301.             break;
  302.  
  303.         }
  304.  
  305.         case keyDown:
  306.         case autoKey:
  307.             key = theEvent->message & charCodeMask;
  308.             if (theEvent->modifiers & cmdKey)
  309.                 if (theEvent->what == keyDown)
  310.                     DoMenuCommand(MenuKey(key));
  311.             break;
  312.  
  313.  
  314.         case kOSEvent:
  315.             switch ((unsigned long) theEvent->message >> 24) {    /**  high byte of message  **/
  316.                  case kSuspendResumeMessage:            /**  suspend/resume is also an activate/deactivate  **/
  317.                     if ((theEvent->message & kResumeMask) == 0)
  318.                        gSleep = 80;                    /** we are headed to the background, so slow down...  **/
  319.                     else
  320.                     {
  321.                        gSleep = 0;                    /** we are headed to the foreground, so speed up...  **/
  322.  
  323. // On a resume event, we need to call GXUpdateJob on all of our documents'
  324. // jobs.  This is important because the user may have just changed
  325. // something which affects our jobs (like the size of the paper in the
  326. // printer).
  327. //
  328. // Since our application stores our document references in the refCon fields
  329. // of our documents' windows, we just loop through every one of our windows,
  330. // extract our document pointers and update the associated jobs.
  331.  
  332.                         whichWindow = FrontWindow();
  333.                         
  334.                         while (whichWindow != nil)
  335.                         {
  336.                             if (((WindowPeek) whichWindow)->windowKind == userKind)
  337.                                 GXUpdateJob(GetDocJob(whichWindow));
  338.                             
  339.                             whichWindow = (WindowPtr) ((WindowPeek) whichWindow)->nextWindow;
  340.                         }
  341.                     }
  342.                 break;
  343.             }
  344.         break;
  345.     }
  346. }
  347.  
  348.  
  349.